﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System.Threading;
using Net.Fly.Article.Util;
using OpenQA.Selenium.Support.UI;
using System.Collections.ObjectModel;

namespace Net.Fly.Article.AutoSubmit
{
    class ArticlecompilationSubmit : AbstractSubmit
    {
        private String m_rootURL = "http://www.articlecompilation.com";
        private String m_memberURL = "http://www.articlecompilation.com/myarticle.php";
        private String m_userName = "flying_book@163.com";
        private String m_password = "bright820813";
        private String m_loginURL = "http://www.articlecompilation.com/login.php";
        private String m_submitURL = "http://www.articlecompilation.com/submitart.php";
        private String m_mainCatagory = "  -  Article Marketing";
        private String m_subCatagory = "Information Technology";
        private String m_title = "Use Free Article Spinner Help Article Write";
        private String m_article = "All of the web masters know how to increase the traffic of the website by writing articles, but the jobs are really boring, they hope there are tools can help them to finish the task efficient. What are the tools? For sure, article spinner is the famous one, sometimes we call it as article re-writer. It can replace the words or phrases in the article with synonymous words, so you can utilize this tool to generate hundreds of unique articles based on one article template. Amazing, right? But lots of these tools are not free,and the payment is really high. And the bad thing is most of tools are desktop applications, after you paid, installed, used, found it is not very useful. What I would like to suggest is using free article spinner online version, save money, get synonymous words from other users, and other benefit. Today I recommend one free article spinner - articlewritebee.com, this tool developed by Tom who is an expert on programming, and very familiar with the technology on writing. You can easily to use it online, no any extra paid, the only thing you need to do is register on Home Page, then you can have fun. After reviews more than thirty article spinner tools, I think it is worthy to write something.Especial my feeling of what the key factors of a good article spinner tool are. Actually the algorithm of article spinner is normal, lots of papers in internet, the most common algorithm is using wordnet that is a large lexical database of English. Nouns, verbs, adjectives and adverbs are grouped into sets of cognitive synonyms (synsets), each expressing a distinct concept. Synsets are interlinked by means of conceptual-semantic and lexical relations. Since most of article spinner use this technology, nothing need to compare. So in my opinion, the first key factor of a good article spinner is price, why? Article spinners use same tech, similar libraries, so the price is the most important, and it's better FREE. What's the secondary? I think auto-increased synonymous library is very important, as you know, there are new idioms, popular words developed in everyday, a good article spinner need a auto-increased synonymous library to support those; And this library can collect user-defined synonymous word to share each others. The last one I only see this feature in Article Spinner Bee, it can change the sequence of sentence, for example, \"When I was five years old, I was fall in love with the neighbor girl\", it can be \"I was fall in love with the neighbor girl When I was five years old\", right, It is totally cool. It can generate the true unique article after spin.";
        private String m_summary = "All of the web masters know how to increase the traffic of the website by writing articles, but the jobs are really boring, they hope there are tools can help them to finish the task efficient. What are the tools? For sure, article spinner is the famous one, sometimes we call it as article re-writer. It can replace the words or phrases in the article with synonymous words, so you can utilize this tool to generate hundreds of unique articles based on one article template.";
        private String m_bio = "Our site focus on providing NO1 services on Article Market, our free article spinner can help article writer to generate unique article in minutes, and users don't need purchase articles any more because we provide spinner articles as well. In the end, you can use our submitter to publish your article in thousands of article directories. Please visit <a href=www.articlewritebee.com/index.php>Article Write Bee</a> Free";
        private String m_bioPlain = "Our site focus on providing NO1 services on Article Market, our free article spinner can help article writer to generate unique article in minutes, and users don't need purchase articles any more because we provide spinner articles as well. In the end, you can use our submitter to publish your article in thousands of article directories. Please visit www.articlewritebee.com/index.php Free";
        private String m_author = "Bright";
        private String m_errMessage = "";
        private String[] m_keywords = { "article spinner", "back link", "SEO" };

        private SubmitStatus m_status = SubmitStatus.NEW;

        private IWebDriver m_driver;


        public ArticlecompilationSubmit()
        {
            m_driver = new FirefoxDriver();
        }

        public override bool Login()
        {
            m_status = SubmitStatus.LOGINING;

            try
            {
                m_driver.Navigate().GoToUrl(m_loginURL);

                IWebElement userInput = WebDriverExtension.FindElement(m_driver, By.Id("uname"), 5);// m_driver.FindElement(By.XPath("//label/input[@name='email']"));
                userInput.SendKeys(m_userName);

                IWebElement passwordInput = WebDriverExtension.FindElement(m_driver, By.Id("pswd"), 5);  // m_driver.FindElement(By.XPath("//label/input[@name='password']"));
                passwordInput.SendKeys(m_password);

                IWebElement submitButton = WebDriverExtension.FindElement(m_driver, By.XPath("//input[@value='Login']"), 5);  //m_driver.FindElement(By.XPath("//form/input[@value='Login']"));
                submitButton.Click();
            }
            catch (System.Exception ex)
            {
                m_errMessage = ex.Message;
                return false;
            }
            
            return true;
        }

        public override bool Submit()
        {
            m_status = SubmitStatus.SUBMITTING;
            try
            {
                m_driver.Navigate().GoToUrl(m_submitURL);

                // Select main Catagory
                SelectElement catagoryMain = new SelectElement(WebDriverExtension.FindElement(m_driver, By.Id("parentId"), 5));
                catagoryMain.SelectByText(m_mainCatagory);
            //    SelectElement catagorySub = new SelectElement(WebDriverExtension.FindElement(m_driver, By.Id("edit-field-article-topic-0-value"), 5));
            //    Thread.Sleep(5000);
           //     catagorySub.SelectByText(m_subCatagory);

                // Set title
                IWebElement titleInput = WebDriverExtension.FindElement(m_driver, By.XPath("//input[@name='f_arttitle']"), 5);
                titleInput.SendKeys(m_title);

                // Set article body
                IWebElement bodyInput = WebDriverExtension.FindElement(m_driver, By.XPath("//textarea[@name='f_artbody']"), 5);
                bodyInput.SendKeys(m_article);

                // Set bio
                IWebElement bioInput = WebDriverExtension.FindElement(m_driver, By.XPath("//textarea[@name='f_artres']"), 5);
                bioInput.SendKeys(m_bio);

                // Set key words
                IWebElement keywordsInput = WebDriverExtension.FindElement(m_driver, By.XPath("//input[@name='f_artkey']"), 5);
                String keys = "";
                for (int i = 0, size = m_keywords.Length; i < size; i++)
                {
                    keys = keys + m_keywords[i];
                    if (i<size-1)
                    {
                        keys = keys + ", ";
                    }
                }
                keywordsInput.SendKeys(keys);


                // Set summary
                IWebElement summaryInput = WebDriverExtension.FindElement(m_driver, By.XPath("//textarea[@name='f_artsummary']"), 5);
                summaryInput.SendKeys(m_summary);

                // Submit
                IWebElement submitButton = WebDriverExtension.FindElement(m_driver, By.XPath("//input[@value='Submit']"), 5);
                submitButton.Click();

                // check error message
                IWebElement errBox = WebDriverExtension.FindElement(m_driver, By.XPath("//table/tbody/tr/td/strong/p"), 5);
                if (errBox != null && errBox.Text.ToLower().Contains("sorry"))
                {
                    m_errMessage = errBox.Text;
                    m_status = SubmitStatus.ERROR;
                    return false;
                }
            }
            catch (System.Exception ex)
            {
                m_errMessage = ex.Message;
                return false;
            }

            m_status = SubmitStatus.SUCCESS;
            return true;
        }

        public override String GetArticleURL()
        {
            if (m_status != SubmitStatus.SUCCESS)
            {
                return "";
            }

            try
            {
                m_driver.Navigate().GoToUrl(m_memberURL);

                // find new article
                ReadOnlyCollection<IWebElement> hrefs = WebDriverExtension.FindElements(m_driver, By.XPath("//td[@id='content']/table/tbody/tr/td/table/tbody/tr"), 5);
                foreach (IWebElement ele in hrefs)
                {
                    if (ele.Text.Contains(m_title))
                        return ele.FindElement(By.XPath("./td[2]/a")).GetAttribute("href");
                }
            }
            catch (System.Exception ex)
            {
                m_errMessage = ex.Message;
            }

            return "";
        }
    }
}
